<?php

class Model_driver extends CI_Model{
	function __construct(){
        parent::__construct();
	}	
	function getMerk(){
		$this->db->select("*");
		$this->db->from("merk");
		$this->db->ORDER_BY("merk_name","ASC");
		$query = $this->db->get();
		if($query->num_rows() > 0){
			$data = array();
			$res = $query->result_array();
			foreach ($res as $hasil){
				$data[] = $hasil;
			}
			return $data;
		}else{
			return array();
		}
	}
	function addMerk($bookn){
		$bookn = strtoupper($bookn);
		$datax = array(
		   	'merk_name' =>$bookn
		);
		$this->db->insert('merk', $datax); 
			
	}
	function getCat(){
		$this->db->select("*");
		$this->db->from("cat");
		$this->db->ORDER_BY("cat_name","ASC");
		$query = $this->db->get();
		if($query->num_rows() > 0){
			$data = array();
			$res = $query->result_array();
			foreach ($res as $hasil){
				$data[] = $hasil;
			}
			return $data;
		}else{
			return array();
		}
	}
	function addCat($bookn){
		$bookn = strtoupper($bookn);
		$datax = array(
		   	'cat_name' =>$bookn
		);
		$this->db->insert('cat', $datax); 
			
	}
	function addBarang(){
		date_default_timezone_set('Asia/Bangkok');
		$data = array(
		   'barcode_id' => $this->input->post("barcode",true) ,
		   'item_name' => $this->input->post("nama_barang",true) ,
		   'item_cat' => $this->input->post("cat",true),
		   'item_merk' => $this->input->post("merk",true),
		   'stok' => $this->input->post("stok",true) ,
		   'date_added' => date("Y-m-d h:i:s")
		   
		);
		$this->db->insert('item', $data);
	}
	
	function getNewlyAdd(){
		$this->db->select("*");
		//$this->db->from("item");
		$this->db->order_by("date_added","desc");
		$query = $this->db->get("item",5);
		if($query->num_rows() > 0){
			$data = array();
			$res = $query->result_array();
			foreach ($res as $hasil){
				$data[] = $hasil;
			}
			return $data;
		}else{
			return array();
		}
		
	}
	
	function getBarangDet($id){
		$this->db->select("*");
		$this->db->from("item");
		$this->db->where("id",$id);
		$query = $this->db->get();
		if($query->num_rows()>0){
			$hasil = $query->result_array();
			return $hasil;
		}
	}
	
	function editBarang($idX){	
		$idA = $this->input->post("hidO",true);
		$data = array(
		   'barcode_id' => $this->input->post("barcode",true),
		   'item_name' => $this->input->post("nama_barang",true) ,
		   'item_cat' => $this->input->post("cat",true),
		   'item_merk' => $this->input->post("merk",true),
		   'stok' => $this->input->post("stok",true)
		   
		);		
		$this->db->where('id', $idA);
		$this->db->update('item', $data);  	
		//$this->db->insert('item', $data);
	}
	function delDataBarang($id){
		$this->db->where('id', $id);
		$this->db->delete('item'); 
	}
	
	function getBarangList(){
		$this->db->select("*");
		$this->db->from("item");
		$query = $this->db->get();
		if($query->num_rows() > 0){
			$data = array();
			$res = $query->result_array();
			foreach ($res as $hasil){
				$data[] = $hasil;
			}
			return $data;
		}else{
			return array();
		}
	}

	function ajaxGetSearchItem($stok,$name,$merk,$cat){
		$str = 'In My Cart : 11 items';
		$int = filter_var($str, FILTER_SANITIZE_NUMBER_INT);
		$search = $pieces = explode(" ", $name);
		$this->db->select("*");
		$this->db->from("item");
		if(($stok=="")&&($merk=="")&&($cat=="")&&($name=="")){ //0000
		}
		elseif(($stok=="")&&($merk=="")&&($cat=="")&&($name!="")){ //0001
			/*foreach($search as $search1){
				$this->db->or_like("name", $search, "match");
			}*/
			$this->db->like("item_name", $name, "match");
		}
		elseif(($stok=="")&&($merk=="")&&($cat!="")&&($name=="")){ //0010
			$this->db->where("item_cat", $cat);
		}
		elseif(($stok=="")&&($merk=="")&&($cat!="")&&($name!="")){ //0011
			$this->db->where("item_cat", $cat);
			$this->db->or_like("item_name", $name, "match");
		}
		elseif(($stok=="")&&($merk!="")&&($cat=="")&&($name=="")){ //0100
			$this->db->where("item_merk", $merk);
		}
		elseif(($stok=="")&&($merk!="")&&($cat=="")&&($name!="")){ //0101
			$this->db->where("item_merk", $merk);
			$this->db->or_like("item_name", $name, "match");
		}
		elseif(($stok=="")&&($merk!="")&&($cat!="")&&($name=="")){ //0110
			$this->db->where("item_merk", $merk);
			$this->db->where("item_cat", $cat);
		}
		elseif(($stok=="")&&($merk!="")&&($cat!="")&&($name!="")){ //0111
			$this->db->where("item_merk", $merk);
			$this->db->where("item_cat", $cat);
			$this->db->or_like("item_name", $name, "match");
		}
		elseif(($stok!="")&&($merk=="")&&($cat=="")&&($name=="")){ //1000
			/*foreach($search as $search1){
				$this->db->or_like("name", $search, "match");
			}*/
			$int = filter_var($stok, FILTER_SANITIZE_NUMBER_INT);
			if($int==""){$int = 0;}
			$this->db->where('stok <', $int);
		}
		elseif(($stok!="")&&($merk=="")&&($cat=="")&&($name!="")){ //1001
			/*foreach($search as $search1){
				$this->db->or_like("name", $search, "match");
			}*/
			$int = filter_var($stok, FILTER_SANITIZE_NUMBER_INT);
			if($int==""){$int = 0;}
			$this->db->where('stok <=', $int);
			$this->db->like("item_name", $name, "match");
		}
		elseif(($stok!="")&&($merk=="")&&($cat!="")&&($name=="")){ //1010
			$int = filter_var($stok, FILTER_SANITIZE_NUMBER_INT);
			if($int==""){$int = 0;}
			$this->db->where('stok <=', $int);
			$this->db->where("item_cat", $cat);
		}
		elseif(($stok!="")&&($merk=="")&&($cat!="")&&($name!="")){ //1011
			$int = filter_var($stok, FILTER_SANITIZE_NUMBER_INT);
			if($int==""){$int = 0;}
			$this->db->where('stok <=', $int);
			$this->db->where("item_cat", $cat);
			$this->db->or_like("item_name", $name, "match");
		}
		elseif(($stok!="")&&($merk!="")&&($cat=="")&&($name=="")){ //1100
			$int = filter_var($stok, FILTER_SANITIZE_NUMBER_INT);
			if($int==""){$int = 0;}
			$this->db->where('stok <=', $int);
			$this->db->where("item_merk", $merk);
		}
		elseif(($stok!="")&&($merk!="")&&($cat=="")&&($name!="")){ //1101
			$int = filter_var($stok, FILTER_SANITIZE_NUMBER_INT);
			if($int==""){$int = 0;}
			$this->db->where('stok <=', $int);
			$this->db->where("item_merk", $merk);
			$this->db->or_like("item_name", $name, "match");
		}
		elseif(($stok!="")&&($merk!="")&&($cat!="")&&($name=="")){ //1110
			$int = filter_var($stok, FILTER_SANITIZE_NUMBER_INT);
			if($int==""){$int = 0;}
			$this->db->where('stok <=', $int);
			$this->db->where("item_merk", $merk);
			$this->db->where("item_cat", $cat);
		}
		elseif(($stok!="")&&($merk!="")&&($cat!="")&&($name!="")){ //1111
			$int = filter_var($stok, FILTER_SANITIZE_NUMBER_INT);
			if($int==""){$int = 0;}
			$this->db->where('stok <=', $int);
			$this->db->where("item_merk", $merk);
			$this->db->where("item_cat", $cat);
			$this->db->or_like("item_name", $name, "match");
		}
				
		 
		  
		$query = $this->db->get();
		if($query->num_rows() > 0){
			$data = array();
			$res = $query->result_array();
			foreach ($res as $hasil){
				$data[] = $hasil;
			}
			return $data;
		}else{
			return array();
		}
	}
	
	
	function getDataSupir($barcode){
		$this->db->select("barcode_id");
		$this->db->from("item");
		$this->db->where("barcode_id", $barcode);
		$query = $this->db->get();
		if($query->num_rows() > 0){
			return false;
		}
		else{
			return true;	
		}
	}
	
	
	
	
	
	function addDriver(){
		$tgllahir = $this->input->post("thn", true). "-" . $this->input->post("tgl", true) . "-" . $this->input->post("tgl", true);
		$data = array(
		   'name' => $this->input->post("nama_supir",true) ,
		   'telp' => $this->input->post("no_telp",true) ,
		   'alamat' => $this->input->post("alamat",true),
		   'no_ktp' => $this->input->post("no_ktp",true),
		   'umur' => $this->input->post("umur",true) ,
		   'gaji' =>$this->input->post("gaji",true),
		   'kategori' => $this->input->post("kategori",true) ,
		   'status' => $this->input->post("status",true) ,
		   'namabel' => $this->input->post("namabel",true),
		   'tempatlahir' => $this->input->post("temlahir",true),
		   'tanggallahir' => $tgllahir ,
		   'telprumah' =>$this->input->post("gaji",true),
		   'email' => $this->input->post("email",true),
		   'emerkontak' => $this->input->post("emerkontak",true) ,
		   'emertelp' =>$this->input->post("emertelp",true)	   
		   
		);
		$this->db->insert('driver', $data); 
		$id = $this->db->insert_id('driver', $data);
		$nid =  "d" . date("ym") . $id;
		$data = array(
			'nid' => $nid
		);
		$this->db->where("driver_id",$id);
		$this->db->update('driver', $data); 
		
		
		
		$data = array(
		   		'driverprice_id' => $id,
				'harga'=> $this->input->post("gaji",true),
				'kode'=> "DDR-" . date("ym") . $id,
				'keterangan' => "Harian"
				
			);
		$this->db->insert('driver_price', $data); 
		
			$data = array(
		   		'driverprice_id' => $id,
				'harga'=> $this->input->post("gajiminggu",true),
				'kode'=> "DWR-" . date("ym") . $id,
				'keterangan' => "Mingguan"
				
			);
		$this->db->insert('driver_price', $data); 
		
			$data = array(
		   		'driverprice_id' => $id,
				'harga'=> $this->input->post("gajibulan",true),
				'kode'=> "DMR-" . date("ym") . $id,
				'keterangan' => "Bulanan"
				
			);
		$this->db->insert('driver_price', $data); 
		
			$data = array(
		   		'driverprice_id' => $id,
				'harga'=> $this->input->post("gaji6",true),
				'kode'=> "D6MR-" . date("ym") . $id,
				'keterangan' => "Enam Bulanan"
				
			);
		$this->db->insert('driver_price', $data); 
		
			$data = array(
		   		'driverprice_id' => $id,
				'harga'=> $this->input->post("gajitahun",true),
				'kode'=> "DYR-" . date("ym") . $id,
				'keterangan' => "Tahuanan"
				
			);
		$this->db->insert('driver_price', $data); 	
		
			$data = array(
		   		'driverprice_id' => $id,
				'harga'=> "0",
				'kode'=> "DDOR-" . date("ym") . $id,
				'keterangan' => "Overtime"
				
			);
		$this->db->insert('driver_price', $data); 	
		
			$data = array(
		   		'driverprice_id' => $id,
				'harga'=> "0",
				'kode'=> "DDLLR-" . date("ym") . $id,
				'keterangan' => "Makan DLL "
				
			);
		$this->db->insert('driver_price', $data); 
	
	}
	
	function getDriverList(){
		$this->db->select("*");
		$this->db->from("driver");
		$query = $this->db->get();
		if($query->num_rows() > 0){
			$data = array();
			$res = $query->result_array();
			foreach ($res as $hasil){
				$data[] = $hasil;
			}
			return $data;
		}else{
			return array();
		}
	}
	
	function ajaxGetSearch($name){
		$this->db->select("*");
		$this->db->from("driver");
		$this->db->like("name", $name, "after");
		$query = $this->db->get();
		if($query->num_rows() > 0){
			$data = array();
			$res = $query->result_array();
			foreach ($res as $hasil){
				$data[] = $hasil;
			}
			return $data;
		}else{
			return array();
		}
	}	
	
	function getDriverDet($id){
		$this->db->select("*");
		$this->db->from("driver");
		$this->db->where("driver_id",$id);
		$query = $this->db->get();
		if($query->num_rows()>0){
			$hasil = $query->result_array();
			return $hasil;
		}
	}
	function getDriverPrice($id){
		$this->db->select("*");
		$this->db->from("driver_price");
		$this->db->where("driverprice_id",$id);
		$query = $this->db->get();
		if($query->num_rows()>0){
			$hasil = $query->result_array();
			return $hasil;
		}
	}
	
	function editDriver($id){			
		$tgllahir = $this->input->post("thn", true). "-" . $this->input->post("bln", true) . "-" . $this->input->post("tgl", true);		
		$data = array(
		   'name' => $this->input->post("nama_supir",true) ,
		   'telp' => $this->input->post("no_telp",true) ,
		   'alamat' => $this->input->post("alamat",true),
		   'no_ktp' => $this->input->post("no_ktp",true),
		   'umur' => $this->input->post("umur",true) ,
		   'gaji' =>$this->input->post("gaji",true),
		   'kategori' => $this->input->post("kategori",true) ,
		   'status' => $this->input->post("status",true) ,
		   'namabel' => $this->input->post("namabel",true),
		   'tempatlahir' => $this->input->post("temlahir",true),
		   'tanggallahir' => $tgllahir ,
		   'telprumah' =>$this->input->post("gaji",true),
		   'email' => $this->input->post("email",true),
		   'emerkontak' => $this->input->post("emerkontak",true) ,
		   'emertelp' =>$this->input->post("emertelp",true)	 		   
		);		
		$this->db->where('driver_id', $id);
		$this->db->update('driver', $data);  	
	}
	
	function delDataDriver($id){
		$this->db->where('driver_id', $id);
		$this->db->delete('driver'); 
	}
	
	
	function addLembur($status='mobil masih disewa'){
		
		$no_invoice = $this->input->post("noInvoiceL");
		
		
		/*tagihanke pelanggan*/$numberTotal = str_replace(".","",$this->input->post("subTotal"));
		/*bayarkesupir*/$numberBayar = str_replace(".","",$this->input->post("deposit"));  
		$dateCreate = strtotime($this->input->post("dateNow",true));
		$dateDue = strtotime($this->input->post("dateDue",true));
		$dateDeliv = strtotime($this->input->post("delivDate",true));
		
		/*periodemulai*/$dateCreate1 = date('Y-m-d', $dateCreate);
		/*periodeakhir*/$dateDue1 = date('Y-m-d', $dateDue);
		$dateDeliv1 = date('Y-m-d', $dateDeliv);
		$datenow = strtotime(date('Y-m-d'));
		$dateDuecompare =strtotime($dateDue1);
		
		if($numberBayar==$numberTotal){
			$status = "Lunas";
			$dueStatus = "Lunas";
		}else if($numberBayar==0){
			$status = "Belum Bayar";
			if($dateDuecompare<$datenow){
				$dueStatus = "Terlambat";
			}else{
				$dueStatus = "Belum Terlambat";
			}
		}else if($numberBayar<$numberTotal){
			$status = "Belum Lunas";
			if($dateDuecompare<$datenow){
				$dueStatus = "Terlambat";
			}else{
				$dueStatus = "Belum Terlambat";
			}
		}
		
		
		
		$data = array(
		   
		   'emplo_id' => $this->session->userdata("user_id"),
		   'cust_id' => $this->input->post("customer",true),
		   'pt_id' => $this->input->post("ptName",true) ,
		   'invoice_id' => $no_invoice,
		   'driver_id' => $this->input->post("salesPerson"),
		   'invo_date' => $dateCreate1,
		   'note' => $this->input->post("note"),
		   'currency' => $this->input->post("mataUang"),
		   'bill' => $this->input->post("billto"),
		   'deliv' => $this->input->post("shipto"),
		   'delivdate' => $dateDeliv1,
		   'duedate' => $dateDue1,
		   'term' => $this->input->post("payterm"),
		   'delivterms' => $this->input->post("delivTerm"),
		   'paymethod' => $this->input->post("payMethod"),
		   'total' => $numberTotal,
		   'bayar' => $numberBayar,
		   'status' => $status,	
		   'hutang' => $this->input->post("hutang"),
		   'due' => $dueStatus,		   
		   'orderno' => $this->input->post("order")
		);
		
		$this->db->insert('lembur', $data); 
		$id_lembur = $this->db->insert_id('lembur', $data);
		$hitung = $this->input->post("counter", true);
		for($i=1;$i<=$hitung;$i++){
			if($this->input->post("hargabrg_".$i, true)==""){}
			else{
				//$prices = number_format($this->input->post("hargabrg_".$i, true), 0, '', ''); 
				
				$dollarValue = $this->input->post("hargabrg_".$i, true); //example of ornery value
				$value = strval(strip_tags($dollarValue)); //figured it couldn't hurt to make it string
				$myval = htmlentities($value); //important part
				$prices = str_replace(array('&amp;#36; ', '.'), '', $myval); //remove $ and comma
				
				$data1 = array(
					'invo_id' => $no_invoice,
					'brg_id' => $this->input->post("kodebrg_".$i, true),
					'brgprt_id' => $this->input->post("kodebrgprt_".$i, true),
					'harga' => $prices,
					'desc' =>  $this->input->post("descbrg_".$i, true),
					'prd' => $this->input->post("q_".$i, true),
					'disc' =>  $this->input->post("disc_".$i, true),
					'tax1' => $this->input->post("tax1_".$i, true),
					'tax2' =>  $this->input->post("tax2_".$i, true),
					'lemburId' =>$id_lembur
				);
				$this->db->insert('lembur_item', $data1); 
			}
		}
		//$this->model_invoice->updateSewa($id_mobil, $id_invo, $tanggal_pinjam, $tanggal_kembali);
	}
	
	
	function checkLembur(){
		$this->db->select("invoice_id");
		$this->db->order_by("invoice_id", "desc");
		
		$this->db->from("lembur");
		$this->db->like('invoice_id', 'LMBR'.date("ym"), 'after');
		$query = $this->db->get();
		$query2 = $query->row_array();
		
		if($query->num_rows==0)
		{			
			
			$jumlah = date("ym")*1000 + 1;
			$jumlah = "LMBR". $jumlah;
			
			return $jumlah;
		}else{
			$angka = explode("LMBR", $query2['invoice_id']);
			$jumlah = $angka[1]+1;
			$jumlah = "LMBR". $jumlah;
			return $jumlah;			
		}
	}
}